let mongoose = require("./db")

// 用户表
let userSchema = new mongoose.Schema({
    username: String,                        //用户名
    password: String,                        //密码
    code: String,                           //员工编号
    email: String,                           //邮箱
    phone: String,                           //手机号
    sex: { type: Number, default: 0 },           //性别  0-未设置 1-男 2-女     
    remark: String,     //备注       
    imgUrl: {                                //头像地址
        type: String,
        default: "https://img.ixintu.com/download/jpg/20210107/af6cdb58ec2fbf0a56036cdf7e72d454_512_512.jpg!bg"
    },
    roleid: { type: mongoose.Types.ObjectId, ref: "role", },   //角色权限id
    status: Number,                          //状态 0-在线，1-离线
    frinds: Array,                           //好友列表，存放好友id
    createTime: { type: Date, default: Date.now },      //创建时间
}, { versionKey: false })

// 好友请求表
let friendRequestSchema = new mongoose.Schema({
    userID: { type: mongoose.Types.ObjectId, ref: "user" },      //用户id 发起好友请求  当前登录用户
    friendID: { type: mongoose.Types.ObjectId, ref: "user" },    //好友id 接受好友请求   想要添加用户
    status: {                                                //状态 0-待处理 1-接收 2-拒绝    
        type: Number,
        default: 0
    },
    created_at: { type: Date, default: Date.now },                //请求发送时间                         
}, { versionKey: false })

// 群聊表
let groupSchema = new mongoose.Schema({
    userID: {
        type: mongoose.Types.ObjectId,        //群主id
        ref: "user"
    },
    name: String,                            //群名称
    imgUrl: {                                //群头像地址    
        type: String,
        default: "https://ts1.cn.mm.bing.net/th/id/R-C.f0ae10220852b04be3e0fc508be97ed4?rik=y8RrukWTXxVL%2bQ&riu=http%3a%2f%2fc.tianhezulin.com%2fcx3%2f2736%2ficon2.jpg&ehk=NQepYYXRXmytXyfB2GybjxwpUsbDhkisYaA7LjrXVF8%3d&risl=&pid=ImgRaw&r=0"
    },
    members: [{                               //群成员列表 
        type: mongoose.Types.ObjectId,        //群成员id
        ref: "user"
    }],
    created_at: { type: Date, default: Date.now },    //群聊创建时间  
}, { versionKey: false })

// 消息表
let messageSchema = new mongoose.Schema({
    sender_id: { type: mongoose.Types.ObjectId, ref: "user" },    //发送者id
    receive_id: { type: mongoose.Types.ObjectId, ref: "user", required: false },   //接收者id
    group_id: { type: mongoose.Types.ObjectId, ref: "lef_group", required: false },   //群id
    content: String,                                                  //消息内容
    context_type: { type: Number, default: 0 },                            //消息类型 0文本  1图片  2表情包
    media_url: { type: String, required: false },                          //存储图片或表情包的链接
    is_read: { type: Boolean, default: false },                            //是否已读 true-已读 false-未读
    created_at: { type: Date, default: Date.now },                         //消息发送时间    
}, { versionKey: false })

// 聊天消息已读未读统计表
let MessageStatusSchema = new mongoose.Schema({
    message_id: { type: mongoose.Types.ObjectId, ref: "lef_message" },    //消息id
    user_id: { type: mongoose.Types.ObjectId, ref: "user" },              //用户id
    is_read: { type: Boolean, default: false },                            //是否已读 true-已读 false-未读
    read_at: { type: Date, default: Date.now },                            //已读时间
})



// 权限表
let roleSchema = new mongoose.Schema({
    name: String,
    menuid: {
        type: [mongoose.Types.ObjectId],
        ref: "menu",
    }
}, { versionKey: false })
// 路由表
let menuSchema = new mongoose.Schema({
    name: String,
    level: Number,
    pid: {
        type: mongoose.Types.ObjectId,
        ref: "menu",
    },
    rank: Number,
    url: String,
}, { versionKey: false })

//token表
let tokenSchema = new mongoose.Schema({
    token: String,
}, { versionKey: false })

// 导出模型
let tokenModel = mongoose.model("token", tokenSchema, "token")
let userModel = mongoose.model("user", userSchema, "user")  //用户表
let roleModel = mongoose.model("role", roleSchema, "role")  //权限表
let menuModel = mongoose.model("menu", menuSchema, "menu")  //路由表
let friendModel = mongoose.model("lef_friend", friendRequestSchema, "lef_friend")               //好友请求表
let groupModel = mongoose.model("lef_group", groupSchema, "lef_group")                          //群聊表
let messagerModel = mongoose.model("lef_message", messageSchema, "lef_message")                //消息表
let MessageStatusModel = mongoose.model("lef_MessageStatus", MessageStatusSchema, "lef_MessageStatus")      //聊天消息已读未读统计表

module.exports = {
    userModel, roleModel, menuModel,tokenModel,
    friendModel, groupModel, messagerModel, MessageStatusModel
}